﻿package com.thorqq.magictimer.timeLoop;


import java.util.Calendar;

import android.media.AudioManager;

import com.thorqq.magictimer.action.ActionKlaxon;
import com.thorqq.magictimer.action.ActionMgr;
import com.thorqq.magictimer.action.ActionVolume;
import com.thorqq.magictimer.core.TAction;
import com.thorqq.magictimer.core.TLoopPolicy;
import com.thorqq.magictimer.core.TTimerDef;
import com.thorqq.magictimer.db.DBHelper;
import com.thorqq.magictimer.util.Util;


public class QuickSetting
{
    static protected Calendar mNow = Calendar.getInstance();
    static protected final String NEW_TIMER_NAME = "新提醒";
    static protected final String NEW_TIMER_REMARK = "备注";

    //************************************************************************//
    // 时间设置
    //************************************************************************//
    /**启动时刻设置
     * @param startHour 时
     * @param startMinute 分
     * @return TTimer
     */
    static public TTimerDef setHourMinute(int startHour, int startMinute)
    {
        int maxCount = 1;
        int intervalMinutes = 5;
        return QuickSetting.setTime(maxCount, intervalMinutes, startHour, startMinute);
    }
    
    /**倒计时
     * @param intervalMinutes 倒计时的分钟数，即在intervalMinutes分钟后启动。
     * @return TTimer
     */
    static public TTimerDef countdownFromNowOn(int intervalMinutes)
    {
        int maxCount = 1;
        mNow = Calendar.getInstance();
        int startHour = mNow.get(Calendar.HOUR_OF_DAY);
        int startMinute = mNow.get(Calendar.MINUTE) + intervalMinutes;
        return QuickSetting.setTime(maxCount, 0, startHour, startMinute);
    }
    
    /**整点报时
     * @return TTimer
     */
    static public TTimerDef fullHour()
    {
        int maxCount = -1;
        int intervalMinutes = 60;
        int startHour = mNow.get(Calendar.HOUR_OF_DAY);
        int startMinute = 0;
        return QuickSetting.setTime(maxCount, intervalMinutes, startHour, startMinute);
    }
    
    /**从现在开始，每隔xx分钟提醒
     * @param intervalMinutes 间隔分钟数
     * @return TTimer
     */
    static public TTimerDef loopBySpecificMinutes(int intervalMinutes)
    {
        int maxCount = -1;
        int startHour = mNow.get(Calendar.HOUR_OF_DAY);
        int startMinute = mNow.get(Calendar.MINUTE);
        return QuickSetting.setTime(maxCount, intervalMinutes, startHour, startMinute);
    }
    
    /** 内部函数
     */
    static public TTimerDef setTime(int maxCount, int intervalMinutes, int startHour, int startMinute)
    {
        int id = DBHelper.getIntance().genTimerID();
        //默认displayOrder等于id
        int enable = 1;
        int lastAlertTime = -1;
        String name = NEW_TIMER_NAME + "-"+ id;
        String remark = "";
        
        TTimerDef t = new TTimerDef(id, id, 
                startHour, startMinute, maxCount, intervalMinutes, lastAlertTime, enable, 
                name, remark);
        return t;
    }    
    
    //************************************************************************//
    // 循环设置
    //************************************************************************//
    
    /**每天循环
     * @return TLoopPolicy
     */
    static public TLoopPolicy everyday()
    {
        return specificDayNumCycle(new char[]{'1'});
    }
    
    /**每周循环
     * @param dayMask 循环日掩码
     * @return TLoopPolicy
     */
    static public TLoopPolicy weekCycle(char[] dayMask)
    {
        TLoopPolicyWeek tlp = new TLoopPolicyWeek(TLoopPolicy.LOOP_POLICY_WEEK, null);

        tlp.setParam(dayMask);

        return tlp;
    }

    /**指定循环天数
     * @param dayMask 循环日掩码
     * @return TLoopPolicy
     */
    static public TLoopPolicy specificDayNumCycle(char[] dayMask)
    {
        TLoopPolicyDay lp = new TLoopPolicyDay(TLoopPolicy.LOOP_POLICY_DAY, null);
        int loopDays = dayMask.length; 
        int maxCount = -1;
        int lunarFlag = 0;
        lp.setParam(loopDays, maxCount, dayMask, mNow, lunarFlag);
        
        return lp;
    }
    
    /**从指定年月日开始持续n天
     * @param countOfDay 倒计时的天数，即在countOfDay天后启动
     * @return TLoopPolicy
     */
    static public TLoopPolicy lastNDaysFromSpecifiedDate(int year, int month, int day, int ndays)
    {
        TLoopPolicyDay lp = new TLoopPolicyDay(TLoopPolicy.LOOP_POLICY_DAY_NDAYS, null);
        
        int loopDays = ndays; 
        int maxCount = 1;
        int lunarFlag = 0;
        char[] dayMask = new char[loopDays];
        for(int i = 0; i < loopDays; i++)
        {
            dayMask[i] = '1';
        }
        
        long tmp = mNow.getTimeInMillis();
        mNow.set(year, month-1, day, 0, 0, 0);
        
        lp.setParam(loopDays, maxCount, dayMask, mNow, lunarFlag);

        mNow.setTimeInMillis(tmp);
        return lp;
    }    

    /**每月几号
     * @param day 日。从1开始
     * @param lunarFlag 是否阴历。1：是，0：否
     * @return TLoopPolicy
     */
    static public TLoopPolicy dayInMonth(int day, int lunarFlag)
    {
        char[] monthMask = new char[TLoopPolicyMon.LENGTH_OF_MONTH_MASK];
        for(int i = 0; i < monthMask.length; i++)
        {
            monthMask[i] = '1';
        }
        
        char[] dayMask = new char[TLoopPolicyMon.LENGTH_OF_DAY_MASK];
        for(int i = 0; i < monthMask.length; i++)
        {
            dayMask[i] = (i == day - 1 ? '1' : '0');
        }
        
        return monthAndDayCycle(monthMask, dayMask, lunarFlag);
    }  
    
    /**指定月日
     * @param month 月。从1开始
     * @param day 日。从1开始
     * @param lunarFlag 是否阴历。1：是，0：否
     * @return TLoopPolicy
     */
    static public TLoopPolicy annualCycle(int month, int day, int lunarFlag)
    {
        char[] monthMask = new char[TLoopPolicyMon.LENGTH_OF_MONTH_MASK];
        for(int i = 0; i < monthMask.length; i++)
        {
            monthMask[i] = (i == month - 1 ? '1' : '0');
        }
        
        char[] dayMask = new char[TLoopPolicyMon.LENGTH_OF_DAY_MASK];
        for(int i = 0; i < dayMask.length; i++)
        {
            dayMask[i] = (i == day - 1 ? '1' : '0');
        }
        
        return monthAndDayCycle(monthMask, dayMask, lunarFlag);
    }    

    /**按月循环，内部按日循环
     * @param monthMask 月掩码
     * @param dayMask 日掩码
     * @param lunarFlag 是否阴历
     * @return TLoopPolicy
     */
    static public TLoopPolicy monthAndDayCycle(char[] monthMask, char[] dayMask, int lunarFlag)
    {
        TLoopPolicyMon lp = new TLoopPolicyMon(TLoopPolicy.LOOP_POLICY_MONTH, null);
                
        char[] weekMask = null;
        char[] weekDayMask = null;
        int subLoopType = TLoopPolicyMon.MONTH_LOOP_SUB_LOOP_DAY;
        lp.setParam(lunarFlag, monthMask, subLoopType, dayMask, weekMask, weekDayMask);

        return lp;
    }

    /**每月的某几日
     * @param dayMask 日掩码
     * @param lunarFlag 是否阴历
     * @return TLoopPolicy
     */
    static public TLoopPolicy monthAndDayCycle(char[] dayMask, int lunarFlag)
    {
        TLoopPolicyMon lp = new TLoopPolicyMon(TLoopPolicy.LOOP_POLICY_MONTH, null);
                
        char[] monthMask = new char[TLoopPolicyMon.LENGTH_OF_MONTH_MASK];
        Util.initializeCharArray(monthMask, '1');
        char[] weekMask = null;
        char[] weekDayMask = null;
        int subLoopType = TLoopPolicyMon.MONTH_LOOP_SUB_LOOP_DAY;
        lp.setParam(lunarFlag, monthMask, subLoopType, dayMask, weekMask, weekDayMask);

        return lp;
    }

    /**按阳历月循环，内部按周循环
     * @param monthMask 月掩码
     * @param weekMask 周掩码
     * @param weekDayMask 周内日掩码
     * @return TLoopPolicy
     */
    static public TLoopPolicy monthAndWeekDayCycle(int month, int week, int weekday)
    {
        char[] monthMask = new char[TLoopPolicyMon.LENGTH_OF_MONTH_MASK];
        char[] weekMask = new char[TLoopPolicyMon.LENGTH_OF_WEEK_MASK];
        char[] weekDayMask = new char[TLoopPolicyMon.LENGTH_OF_WEEK_DAY_MASK];
                
        for(int i = 0; i < monthMask.length; i++)
        {
            monthMask[i] = (i == month - 1 ? '1' : '0');
        }

        for(int i = 0; i < weekMask.length; i++)
        {
            weekMask[i] = (i == week - 1 ? '1' : '0');
        }

        for(int i = 0; i < weekDayMask.length; i++)
        {
            weekDayMask[i] = (i == weekday - 1 ? '1' : '0');
        }

        return monthAndWeekDayCycle(monthMask, weekMask, weekDayMask);
    }

    static public TLoopPolicy monthAndWeekDayCycle(char[] monthMask, char[] weekMask, char[] weekDayMask)
    {
        TLoopPolicyMon lp = new TLoopPolicyMon(TLoopPolicy.LOOP_POLICY_MONTH, null);
                
        char[] dayMask = null;
        int lunarFlag = 0;
        int subLoopType = TLoopPolicyMon.MONTH_LOOP_SUB_LOOP_WEEK;
        
        lp.setParam(lunarFlag, monthMask, subLoopType, dayMask, weekMask, weekDayMask);

        return lp;
    }

    /**每月的某几个周几
     * @param weekMask 周掩码
     * @param weekDayMask 周内日掩码
     * @return TLoopPolicy
     */
    static public TLoopPolicy monthAndWeekDayCycle(char[] weekMask, char[] weekDayMask)
    {
        TLoopPolicyMon lp = new TLoopPolicyMon(TLoopPolicy.LOOP_POLICY_MONTH, null);
                
        char[] monthMask = new char[TLoopPolicyMon.LENGTH_OF_MONTH_MASK];
        Util.initializeCharArray(monthMask, '1');
        char[] dayMask = null;
        int lunarFlag = 0;
        int subLoopType = TLoopPolicyMon.MONTH_LOOP_SUB_LOOP_WEEK;
        
        lp.setParam(lunarFlag, monthMask, subLoopType, dayMask, weekMask, weekDayMask);

        return lp;
    }
            
    /**指定年月日。一次性的
     * @param year 年
     * @param month 月。从1开始
     * @param day 日。从1开始
     * @param lunarFlag 是否阴历。1：是，0：否
     * @return TLoopPolicy
     */
    static public TLoopPolicy specifiedDate(int year, int month, int day, int lunarFlag)
    {
        int maxCount = 1;
        return setSpecificDay(year, month, day, maxCount, lunarFlag);
    }
    
    /** 内部函数
     */
    static protected TLoopPolicy setSpecificDay(int year, int month, int day, int maxCount, int lunarFlag)
    {
        TLoopPolicyDay tlp = new TLoopPolicyDay(TLoopPolicy.LOOP_POLICY_DAY, null);
        char[] dayMask = new char[]{'1'};
        int loopDays = dayMask.length; 

        long tmp = mNow.getTimeInMillis();
        mNow.set(year, month-1, day, 0, 0, 0);
        
        tlp.setParam(loopDays, maxCount, dayMask, mNow, lunarFlag);
        mNow.setTimeInMillis(tmp);

        return tlp;
    }
    
    //************************************************************************//
    // 动作设置
    //************************************************************************//
    static public TAction klaxon()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_KLAXON,ActionKlaxon.DEFAULT_MUSIC);
    }
    
    static public TAction vibrator()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_VIBRATOR);
    }

    static public TAction notification()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_NOTIFY);
    }

    static public TAction dialog()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_DIALOG);
    }

    static public TAction airplaneMode(int open)
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_AIRPLANE_MODE, Util.intToStr(open));
    }

    static public TAction EnterAirplaneMode()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_AIRPLANE_MODE, Util.intToStr(1));
    }

    static public TAction LeaveAirplaneMode()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_AIRPLANE_MODE, Util.intToStr(0));
    }

    static public TAction stopProcess()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_STOP_PROCESS);
    }

    static public TAction ringerMode(int mode)
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_RINGER_MODE, 
                Util.intToStr(mode));
    }

    static public TAction ringerNormal()
    {
        return ringerMode(AudioManager.RINGER_MODE_NORMAL);
    }

    static public TAction ringerSilent()
    {
        return ringerMode(AudioManager.RINGER_MODE_SILENT);
    }

    static public TAction ringerVibrate()
    {
        return ringerMode(AudioManager.RINGER_MODE_VIBRATE);
    }

    static public TAction volume(int ring, int music, int notification)
    {
        ActionVolume action = (ActionVolume)ActionMgr.getIntance().createAction(ActionMgr.ACTION_VOLUME);
        action.setParam(ring, music, notification);
        return action;
    }

//    static public TAction volume(Context context)
//    {
//        AudioManager audioMgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
//        int volRing = audioMgr.getStreamVolume(AudioManager.STREAM_RING);
//        int volMusic = audioMgr.getStreamVolume(AudioManager.STREAM_MUSIC);
//        int volNotification = audioMgr.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
//
//        ActionVolume action = (ActionVolume)ActionMgr.getIntance().createAction(ActionMgr.ACTION_VOLUME);
//        action.setParam(volRing, volMusic, volNotification);
//        return action;
//    }

    static public TAction sendSMS()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_SEND_SMS);
    }

    static public TAction sendVirtualSMS()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_SEND_VIRTUAL_SMS);
    }

    static public TAction recvVirtualSMS()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_RECV_VIRTUAL_SMS);
    }

    static public TAction phone()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_PHONE);
    }

    static public TAction deleteSMS()
    {
        return ActionMgr.getIntance().createAction(ActionMgr.ACTION_DELETE_SMS);
    }

}

